package com.freeSoft;

import java.io.File;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DealSpringConfig {
	private String tableName;
	private Properties prop;
	private Document doc;
	private String springPath ;
	private String strutsPath ;
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Properties getProp() {
		return prop;
	}

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	
	public void addBeanToSpringConfig(){
		loadApplicationContext();
		addDao();
		addService();
		saveApplicationContext();
		loadStruts();
		addStruts();
		saveStruts();
	}
	
	private void loadStruts(){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		String outputDir = Util.getOutputDir(prop);
		String projectName = prop.getProperty(Util.PROJECT_NAME);
		String webinfPath = outputDir+ projectName + File.separator+ projectName+ Util.WEB_NAME +File.separator+ 
		"src"+File.separator+"main"+File.separator+"resources" +File.separator;
		strutsPath =webinfPath+ "struts-"+projectName+".xml";
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(strutsPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void addStruts(){
		String projectName = prop.getProperty(Util.PROJECT_NAME);
		 tableName = tableName.substring(0,1).toLowerCase()+tableName.substring(1, tableName.length());
		 Element bean=doc.createElement("action");
		 bean.setAttribute("name",Util.changeName( tableName));
		 bean.setAttribute("class", Util.changeName( tableName));
//		 <result name="success">/machineMap/view/index.jsp</result>
		 Element property=doc.createElement("result");
		 property.setAttribute("name", "success");
		 property.appendChild(doc.createTextNode("/"+projectName+"/"+Util.changeName( tableName)+".jsp"));
		 bean.appendChild(property);
		 
		 Element addBean=doc.createElement("action");
		 addBean.setAttribute("name",Util.changeInitialToUpper( Util.changeName( tableName)));
		 addBean.setAttribute("class", Util.changeName( tableName));
		 Element addProperty=doc.createElement("result");
		 addProperty.setAttribute("name", "success");
		 addProperty.appendChild(doc.createTextNode("/"+projectName+"/add"+Util.changeInitialToUpper( Util.changeName( tableName))+".jsp"));
		 addBean.appendChild(addProperty);
		 
		 Element editBean=doc.createElement("action");
		 editBean.setAttribute("name",Util.changeName( "update"+Util.changeInitialToUpper( tableName)) );
		 editBean.setAttribute("class", Util.changeName( tableName));
		 Element editProperty=doc.createElement("result");
		 editProperty.setAttribute("name", "success");
		 editProperty.appendChild(doc.createTextNode("/"+projectName+"/update"+Util.changeInitialToUpper( Util.changeName( tableName))+".jsp"));
		 editBean.appendChild(editProperty);
		 
		 Element deleteBean=doc.createElement("action");
		 deleteBean.setAttribute("name",Util.changeName( "delete"+Util.changeInitialToUpper( Util.changeName( tableName) )) );
		 deleteBean.setAttribute("class", Util.changeName( tableName));
		 Element deleteProperty=doc.createElement("result");
		 deleteProperty.setAttribute("name", "success");
		 deleteProperty.appendChild(doc.createTextNode("/"+projectName+"/list"+Util.changeInitialToUpper( Util.changeName( tableName))+".jsp"));
		 deleteBean.appendChild(deleteProperty);
		 
		 NodeList nl =doc.getElementsByTagName("package");
		 boolean b = false;
		 Node n=null;
		 for(int i=0;i<nl.getLength();i++){
			 NamedNodeMap nm= nl.item(i).getAttributes();
			 for( int j=0;j<nm.getLength();j++){
				 if (nm.item(j).getNodeName().equalsIgnoreCase(projectName));{
					 b = true;
					 n = nl.item(i);
					 break;
				 }
			 }
			 if (b)
				 break;
		 }
		 n.appendChild(bean);
		 n.appendChild(addBean);
		 n.appendChild(editBean);
		 n.appendChild(deleteBean);
	}
	
	private void saveStruts(){
		  TransformerFactory factory=TransformerFactory.newInstance();
		  try {
		   Transformer tran=factory.newTransformer();
		   Source xml=new DOMSource(doc);
		   Result result=new StreamResult(strutsPath);
		   tran.setOutputProperty(OutputKeys.INDENT,"yes");  
		   tran.transform(xml,result);
		  } catch (Exception e) {
			  e.printStackTrace();
		  }
	}
	
	private void addDao(){
		 Element bean=doc.createElement("bean");
		 String daoImplPath = prop.getProperty(Util.PROP_DAO_IMPL_PATH);
		 String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		 beanName = Util.changeName(beanName);	 
		 tableName = tableName.substring(0,1).toLowerCase()+tableName.substring(1, tableName.length());
		 bean.setAttribute("id",Util.changeName( tableName+"Dao"));
		 String classPath = daoImplPath+"."+beanName+"DaoImpl";
		 bean.setAttribute("class", classPath);
		 Element property=doc.createElement("property");
		 property.setAttribute("name", "jdbcTemplate");
		 property.setAttribute("ref", "jdbcTemplate");
		 bean.appendChild(property);
		 doc.getDocumentElement().appendChild(bean);
	}
	
	private void addService(){
		Element bean=doc.createElement("bean");
		String serviceImplPath = prop.getProperty(Util.PROP_SERVICE_IMPL_PATH);
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		beanName = Util.changeName(beanName);	 
		tableName = tableName.substring(0,1).toLowerCase()+tableName.substring(1, tableName.length());
		bean.setAttribute("id",Util.changeName( tableName)+"Service");
		String classPath = serviceImplPath+"."+beanName+"ServiceImpl";
		bean.setAttribute("class", classPath);
		Element property=doc.createElement("property");
		property.setAttribute("name", tableName+"Dao");
		property.setAttribute("ref", tableName+"Dao");
		bean.appendChild(property);
		doc.getDocumentElement().appendChild(bean);
	}
	
	private void saveApplicationContext(){
		  TransformerFactory factory=TransformerFactory.newInstance();
		  try {
		   Transformer tran=factory.newTransformer();
		   Source xml=new DOMSource(doc);
		   Result result=new StreamResult(springPath);
		   tran.setOutputProperty(OutputKeys.INDENT,"yes");  
		   tran.transform(xml,result);
		  } catch (Exception e) {
			  e.printStackTrace();
		  }
	}
	private void loadApplicationContext() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		String outputDir = Util.getOutputDir(prop);
		String projectName = prop.getProperty(Util.PROJECT_NAME);
		String webinfPath = outputDir+ projectName + File.separator+ projectName+ Util.WEB_NAME +File.separator+ 
		"src"+File.separator+"main"+File.separator+"webapp"+File.separator+"WEB-INF"+File.separator;
		springPath =webinfPath+ "applicationContext.xml";
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(springPath);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
